//
//  FMsearchXiangViewController.m
//  MountEverestFM
//
//  Created by dllo on 15/11/8.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "FMsearchXiangViewController.h"
#import "FMSearchXiangTableViewCell.h"
#import "SortDetailedTVC.h"
#import "SortDetailedGDTableViewCell.h"
#import "SearchXiang.h"
#import "MusicListViewController.h"
#import "PlayerViewController.h"
#import "Player.h"
@interface FMsearchXiangViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong)UITableView *tableView;
@property (nonatomic ,strong)NSMutableArray *arr1;
@property (nonatomic ,strong)NSMutableArray *arr2;
@property (nonatomic ,strong)NSMutableDictionary *dic;
@end

@implementation FMsearchXiangViewController
-(void)netWorking
{
    [NetWorkManager GET:[NSString stringWithFormat:@"http://search.ximalaya.com/front/v1?core=all&device=iPhone&kw=%@&page=1&rows=3",self.str] isCaChe:YES resultData:^(id responseObject) {
      self.dic=responseObject;
      NSMutableArray *arr=[[responseObject objectForKey:@"album"] objectForKey:@"docs"];
        self.arr1=[NSMutableArray array];
        for (NSMutableDictionary *dic in arr) {
            SearchXiang *search=[[SearchXiang alloc]init];
            [search setValuesForKeysWithDictionary:dic];
            [self.arr1 addObject:search];

        }
        self.arr2=[NSMutableArray array];
        NSMutableArray *arr1=[[responseObject objectForKey:@"track"] objectForKey:@"docs"];
        for (NSMutableDictionary *dic in arr1) {
            SearchXiang *search=[[SearchXiang alloc]init];
            [search setValuesForKeysWithDictionary:dic];
            [self.arr2 addObject:search];
        }
        [self.tableView reloadData];
    } resultRrror:^(NSError *error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self netWorking];
    
    // Do any additional setup after loading the view.
    self.tableView =[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = UIColorFromRGB(0xe7e7e7);
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.view addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.arr1.count !=0 && self.arr2.count != 0) {
        return 2;
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return self.arr1.count;
    }else{
        return self.arr2.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return FM_Screen_Pts_Height*50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        // 声音
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Height*50)];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(FM_Screen_Pts_Width*30, 0, FM_Screen_Pts_Width*100, FM_Screen_Pts_Height*50)];
        titleLabel.text = @"声音";
        titleLabel.font = [UIFont systemFontOfSize:15];
        [view addSubview:titleLabel];
        return view;
    }else{
        // 专辑
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Height*50)];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(FM_Screen_Pts_Width*30, 0, FM_Screen_Pts_Width*100, FM_Screen_Pts_Height*50)];
        titleLabel.text = @"专辑";
        titleLabel.font = [UIFont systemFontOfSize:15];
        [view addSubview:titleLabel];
        return view;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
            SortDetailedTVC *cell=[tableView dequeueReusableCellWithIdentifier:@"zhengeren"];
            if (!cell) {
                cell=[[SortDetailedTVC alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"zhengeren"];
            }
            SearchXiang *xiang=[self.arr1 objectAtIndex:indexPath.row];
            cell.lable1.text=xiang.title;
            cell.lable2.text=xiang.intro;
            [cell.imageV1 sd_setImageWithURL:[NSURL URLWithString:xiang.cover_path] placeholderImage:[UIImage imageNamed:@"find_kind_btn_default@3x.png"]];
            cell.imageV2.image=[UIImage imageNamed:@"sound_playtimes@3x"];
            cell.imageV3.image=[UIImage imageNamed:@"album_tracks@3x.png"];
            
            cell.imV.image=[UIImage imageNamed:@"cell_arrow_all.png"];
            
            CGFloat f=[xiang.play floatValue];
            if (f<10000) {
                cell.lable3.text=[NSString stringWithFormat:@"%.0f",f];
            }else{
                cell.lable3.text=[NSString stringWithFormat:@"%.2f万",f/10000];
            }
            NSString  *str=[xiang.tracks stringValue];
            cell.lable4.text=[str stringByAppendingString:@"集"];
            return cell;
    }else{
            SortDetailedTVC *cell=[tableView dequeueReusableCellWithIdentifier:@"zhengeren1"];
            if (!cell) {
                cell=[[SortDetailedTVC alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"zhengeren1"];
            }
            SearchXiang *xiang=[self.arr2 objectAtIndex:indexPath.row];
            cell.lable1.text=xiang.title;
            cell.lable2.text=xiang.nickname;
            [cell.imageV1 sd_setImageWithURL:[NSURL URLWithString:xiang.cover_path] placeholderImage:[UIImage imageNamed:@"find_kind_btn_default@3x.png"]];
            cell.imageV2.image=[UIImage imageNamed:@"sound_playtimes@3x"];

            
            cell.imV.image=[UIImage imageNamed:@"cell_arrow_all.png"];
            
            CGFloat f=[xiang.count_play floatValue];
            if (f<10000) {
                cell.lable3.text=[NSString stringWithFormat:@"%.0f",f];
            }else{
                cell.lable3.text=[NSString stringWithFormat:@"%.2f万",f/10000];
            }
//            NSString  *str=[xiang.tracks stringValue];
//            cell.lable4.text=[str stringByAppendingString:@"集"];
            return cell;
    }
        return NULL;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return FM_Screen_Pts_Width*70+FM_Screen_Pts_Height*40;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
            MusicListViewController *mu=[[MusicListViewController alloc]init];
            mu.musicListDic=[[[self.dic objectForKey:@"album"] objectForKey:@"docs"] objectAtIndex:indexPath.row];
            [self.navigationController pushViewController:mu animated:YES];
    }else{
            Player *player = [Player shareInstance];
            player.striDic=self.dic;
            player.listArr=[[self.dic objectForKey:@"track"] objectForKey:@"docs"];
            [player playFromNet];
            self.tabBarController.selectedIndex = 2;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
